package com.ybu;

import java.util.HashMap;
import java.util.Map;


/**
 * @ProjectName: shixun
 * @Titile: MapPractice
 * @Author: Jie
 * @Description: TODO
 */
public class MapPractice {
    public static void main(String[] args) {

        Map<String, String> data = new HashMap<>();
        data.put("张三","A");
        data.put("李四","B");
        data.put("王五","C");
        data.put("赵六1","D");
        data.put("李六","A");
        data.put("赵六","D");
        data.put("张三1","A");
        data.put("张三2","B");
        data.put("张三3","C");
        data.put("张三4","A");
        data.put("张三5","A");
        data.put("张三6","C");
        data.put("张三7","A");
        data.put("张三8","C");
        data.put("张三9","A");
        data.put("张三10","C");
        data.put("张三11","A");
        //调用方法统计
        count(data);

    }
    public static void count(Map<String,String> data){
        Map<String,Integer> resultMap = new HashMap<>();
        data.forEach((k,v)->{
            //判断v是否在resultMap中已经存在
            boolean containsKey = resultMap.containsKey(v);
            if(containsKey){
                //如果存在，则v对应的value值加1
                Integer oldCount = resultMap.get(v);
                resultMap.put(v,oldCount+1);
            }else{
                //如果不存在，则将v这个值作为key，value（1）作为value存储到新的集合中
                resultMap.put(v,1);
            }
        });
        //遍历新的map集合，打印出统计结果
        resultMap.forEach((k,v)->{
            System.out.println(k+"-->"+v);
        });
    }
}
